"""
命名元组nametuple的使用示例
"""

from collections import namedtuple

# 使用nametuple构建User类
User = namedtuple("User", ["name", "no", "height", "address"])

if __name__ == '__main__':
    user = User(name="Kevin", no=89757, height=173, address="杭州")
    print(user.name, user.no, user.height, user.address)

    # 使用元组初始化User
    user_tuple = ("Kevin", 89757, 173)
    user = User(*user_tuple, "杭州")
    print(user.name, user.no, user.height, user.address)

    # 使用字典初始化User
    user_dict = {
        "name": "Kevin",
        "no": 89757,
        "height": 173,
    }
    user = User(**user_dict, address="长沙")
    print(user.name, user.no, user.height, user.address)

    # 将nametuple转换成dict
    user_info_dict = user._asdict()
    print(user_info_dict['name'], user_info_dict['no'], user_info_dict['height'], user_info_dict['address'])
